package com.jae.controller.user;

import com.jae.controller.BaseController;
import com.jae.model.ResultMsg;
import com.jae.model.ServiceRuntimeException;
import com.jae.model.user.User;
import com.jae.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/admin/user")
public class UserAdminController extends BaseController {

    @Autowired
    private UserService userService;

    @GetMapping("/info")
    public ResultMsg info(HttpServletRequest request ) {
        String interfaceName = "用户信息";
        try {
            User user = getUser();
            user.setPassword(null);
            return new ResultMsg(true,user) ;
        } catch (ServiceRuntimeException e){
            return fail(e);
        } catch (Exception e){
            return error(interfaceName,e,request);
        }
    }

    @PostMapping("/logout")
    public ResultMsg logout(HttpServletRequest request) {
        String interfaceName = "退出登录";
        try {
            User user = getUser();
            userService.logout(user);
            return new ResultMsg(true) ;
        } catch (ServiceRuntimeException e){
            return fail(e);
        } catch (Exception e){
            return error(interfaceName,e,request);
        }
    }
}
